/**
 * 
 */
package com.qdairlines.service.dict.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qdairlines.common.TraceUtil;
import com.qdairlines.constant.BaseConsts;
import com.qdairlines.dao.dict.DictionaryMapper;
import com.qdairlines.entity.dict.Dictionary;
import com.qdairlines.entity.dict.DictionaryItem;
import com.qdairlines.entity.dict.DictionaryView;
import com.qdairlines.service.base.impl.BaseServiceImpl;
import com.qdairlines.service.dict.DictionaryItemService;
import com.qdairlines.service.dict.DictionaryService;

/**
 * @author pangyannan
 * @date 2017年3月6日
 * @company QingDao Airlines
 * @description 字典服务实现
 */
@Service
public class DictionaryServiceImpl extends BaseServiceImpl<Dictionary,Integer> implements DictionaryService {
	private static final Logger logger = LoggerFactory.getLogger(DictionaryServiceImpl.class);
	@Autowired
	private DictionaryMapper dictionaryMapper;
	@Autowired
	private DictionaryItemService dictionaryItemService;
	
	
	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(dictionaryMapper);
	}


	@Override
	public List<DictionaryView> listDictionaryView(String dictionaryCode) {
		return dictionaryMapper.listDictionView(dictionaryCode);
	}


	@Override
	public String getSelect2Json(String dictionaryCode) {
		List<DictionaryView> list = this.listDictionaryView(dictionaryCode);
		if(list !=null && list.size()>0){
			StringBuffer sb = new StringBuffer();
			sb.append("[");
			for(DictionaryView d :list){
				sb.append("{ id:");
				sb.append(d.getId());
				sb.append(", text: '");
				sb.append(d.getText());
				sb.append("' },");
			}
			sb.deleteCharAt(sb.length() - 1);
			sb.append("]");
			return sb.toString();
		}else{
			return null;
		}
	}

	@Override
	public String getDefaultVal(String dictionaryCode) {
		return dictionaryMapper.getDefaultVal(dictionaryCode);
	}
	
	@Override
	@Transactional
	public  int deleteByPrimaryKey(Integer dictionaryId){
		DictionaryItem di = new DictionaryItem();
		di.setDictionaryId(dictionaryId);
		List<DictionaryItem> diList = dictionaryItemService.listData(di);
		if(diList !=null && diList.size()>0){
			for(DictionaryItem d: diList){
				dictionaryItemService.deleteByPrimaryKey(d.getId());
			}
			
		}
		dictionaryMapper.deleteByPrimaryKey(dictionaryId);
		return 1;
	}



	@Override
	public List<DictionaryView> listDictionViewAll(String dictionaryCode,
			String language) {
		List<DictionaryView>  list  = dictionaryMapper.listDictionViewAll(dictionaryCode);
		if(list !=null && list.size()>0)
		if(BaseConsts.EN_US.equals(language)){
			for(DictionaryView d:list){
				if(!StringUtils.isEmpty(d.getTextEn())){
					d.setText(d.getTextEn());
				}
				d.setTextEn(null);
			}
		}
		return list;
	}



	@Override
	public <T> List<T> convertDictValueToText(List<T> list, String valueName,
			String textName,String dictionaryCode, String language) {
		if(list !=null && list.size() > 0){
			List<DictionaryView> dictList = this.listDictionViewAll(dictionaryCode, language);
			for(T t:list){
				String value = null;
				try {
					value = BeanUtils.getProperty(t,valueName);
					if(StringUtils.isEmpty(value)){
						continue;
					}
					String text = this.getDictText(value, dictList);
					BeanUtils.setProperty(t, textName, text);
				} catch (Exception e) {
					TraceUtil.error(logger, e);
				} 
			}
			
		}
		
		return list;
	}
	
	/**
	 * 根据字典编码值，返回字典显示值，多个返回第一个
	 * @param value
	 * @param list
	 * @return
	 */
	private String getDictText(String value,List<DictionaryView>  list){
		for(DictionaryView d:list){
			if(value.equals(d.getId())){
				return d.getText();
			}
		}
		return value;
	}
	
}
